package com.chinanet.controller;

import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ShutdownController implements ApplicationContextAware {

    private ApplicationContext context;

    @PostMapping("/shutdown")
    public void shutdownContext(){
        ((ConfigurableApplicationContext)context).close();
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.context = ctx;

    }

    @PostMapping("/close")
    public void AppClose(){
        SpringApplication.exit(((ConfigurableApplicationContext)context),()-> 0);
    }

    @PostMapping("/tomcat/close")
    public void processClose(){
        System.out.println("次方法未实现关闭方法，请使用POST方法发送shutdown或者close进行安全的应用程序关闭");
    }

}
